package com.atguigu.map;

import com.atguigu.set.FileOperation;

import java.util.ArrayList;


public class WordFrequencyE {

    public static double testMap(String filename, Map<String, Integer> map) {
        double startTime=System.nanoTime();
        System.out.println(filename);
        ArrayList<String> words = new ArrayList<>();
        if (FileOperation.readFile(filename, words)) {
            System.out.println("Total words：" + words.size());
            for (String word : words) {
                Integer count = map.get(word);
                map.add(word, count == null ? 1 : count + 1);
            }
            System.out.println("Total different words: " + map.getSize());
            System.out.println("Frequency of PRIDE: " + map.get("pride"));
            System.out.println("Frequency of PREJUDICE: " + map.get("prejudice"));
        }
        double endTime=System.nanoTime();
        return (endTime-startTime)/1000000000.0;
    }

    /***
     * pride-and-prejudice.txt
     * Total words：125901
     * Total different words: 6530
     * Frequency of PRIDE: 53
     * Frequency of PREJUDICE: 11
     * BST Map: 0.1473564 s
     * ===========================================
     * pride-and-prejudice.txt
     * Total words：125901
     * Total different words: 6530
     * Frequency of PRIDE: 53
     * Frequency of PREJUDICE: 11
     * Lined List Map: 7.2139835 s
     * ===========================================
     * pride-and-prejudice.txt
     * Total words：125901
     * Total different words: 6530
     * Frequency of PRIDE: 53
     * Frequency of PREJUDICE: 11
     * AVL Map: 0.0745823 s
     */
    public static void main(String[] args) {
        String filename="pride-and-prejudice.txt";
        BSTMap<String,Integer>map1=new BSTMap<>();
        double time1 = testMap(filename, map1);
        System.out.println("BST Map: "+time1+" s");
        System.out.println("===========================================");
        LinkedListMap<String,Integer>map2=new LinkedListMap<>();
        double time2=testMap(filename,map2);
        System.out.println("Lined List Map: "+time2+" s");
        System.out.println("===========================================");
        AVLMap<String,Integer>avl=new AVLMap<>();
        double time3 = testMap(filename, avl);
        System.out.println("AVL Map: "+time3+" s");




    }
}
